//
//  OnLineTeacherDetailVC.m
//  GC_OC
//
//  Created by lbh on 2022/4/8.
//

#import "OnLineTeacherDetailVC.h"
#import "TeadcherHeadView.h"
#import "OnLieTeacherDetailListVC.h"
#import "EmptyView.h"

@interface OnLineTeacherDetailVC ()
@property (nonatomic, weak) UIView *navView;
@property (nonatomic,strong)TeadcherHeadView *headBgView;;
@property (nonatomic, strong)ObjectAFNetWorking* objectAFNet;
@property (nonatomic,strong)UIView *navBarView;
@property (nonatomic,strong)EmptyView *emptyView;
@property (nonatomic,strong)NSArray *dataSource;
@end

@implementation OnLineTeacherDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.tintColor=AppMainColor;
    [self setNeedsStatusBarAppearanceUpdate];
    [self creatHeadView];
    [self getTearchListData];
}
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.tintColor=AppMainColor;
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
//}
//-(void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
//导航栏设置
-(void)creatHeadView{
    
    self.objectAFNet=[[ObjectAFNetWorking alloc]init];
    self.view.backgroundColor = UIColor.whiteColor;
    
    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,topHeight)];
    self.navView = view;
    view.backgroundColor = AppMainColor;
    CGFloat titleHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)] andWidth:wid(100)];
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(95),StatusBarHeight+(topHeight-StatusBarHeight-titleHeight)/2.0,SCREEN_WIDTH-wid(65)*2,titleHeight)];
    titleLabel.textColor=[UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)]];
    titleLabel.textAlignment=NSTextAlignmentLeft;
    titleLabel.text = self.dataDict[@"teacher_name"];;
    [view addSubview:titleLabel];
    self.navBarView = view;
    
    UIButton* leftbtn=[UIButton new];
    leftbtn.frame=CGRectMake(wid(20),StatusBarHeight+(topHeight-StatusBarHeight-wid(40))/2.0,wid(40),wid(40));
    [leftbtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftbtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [leftbtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftbtn];
    [self.view addSubview:view];
    
    UILabel* sexLabel=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 40,StatusBarHeight+(topHeight-StatusBarHeight-titleHeight)/2.0,SCREEN_WIDTH-wid(65)*2,titleHeight)];
    sexLabel.textColor=[UIColor whiteColor];
    [sexLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)]];
    sexLabel.textAlignment=NSTextAlignmentLeft;
    NSString *teacher_sex = self.dataDict[@"teacher_sex"];
    sexLabel.text = teacher_sex.intValue ==2?@"女":@"男";
    [view addSubview:sexLabel];
    
    self.emptyView.bEmptyLabel.text = @"暂无数据";
    self.emptyView.bEmptyImageView.image = [UIImage imageNamed:@"无数据"];
    [self.view addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
    }];
}
//分页设置
- (void)createPageView{
    NSArray *titleArray = [self getTabHeadViewAllTitle];
    NSMutableArray *data = [[NSMutableArray alloc]init];
    for (NSString *str in titleArray) {
        NSDictionary *dict = @{WMZPageKeyName:str,WMZPageKeyTitleMarginY:@(0)};
        [data addObject:dict];
    }
    self.emptyView.hidden = titleArray.count != 0;
    //标题数组  pageLooView
    WMZPageParam *param = PageParam();
    param.wMenuImagePositionSet(PageBtnPositionLeft)
        .wMenuTitleColorSet(UIColor.blackColor)
        .wMenuTitleSelectColorSet(UIColor.blackColor)
        .wMenuTitleUIFontSet([UIFont systemFontOfSize:18])
        .wMenuTitleSelectUIFontSet([UIFont boldSystemFontOfSize:18])
        .wTitleArrSet(data)
//        .wMenuIndicatorColorSet(AppMainColor)
        .wMenuIndicatorColorSet(UIColor.clearColor)
        .wMenuIndicatorWidthSet(30)
        .wMenuIndicatorRadioSet(2)
        .wMenuHeightSet(50)
        .wMenuIndicatorYSet(3)
//        .wMenuBottomMarginYSet(10)
    .wMenuSelectTitleBackgroundSet(UIColor.whiteColor)
    //控制器数组
        .wViewControllerSet(^UIViewController *(NSInteger index) {
            OnLieTeacherDetailListVC *vc = OnLieTeacherDetailListVC.new;
            vc.page = index;
            if (index ==   0) {
                vc.classArray = [self getClassContentArray:index];
            }
            else{
                vc.classArray = [self getClassContentArray:index - 1];
            }
            vc.medicallID = self.medilassID;
            return (UIViewController*)vc;
        })
//        .wMenuTitleWidthSet(PageVCWidth/titleArray.count)
        .wMenuAnimalSet(PageTitleMenuPDD)
    //菜单view设置
//        .wMenuBgColorSet(UIColor.whiteColor)
        .wMenuBgColorSet(AppMainGrayColor)
    //悬浮开启
        .wTopSuspensionSet(YES)
    
    //顶部可下拉
        .wBouncesSet(NO)
    //头视图y坐标从导航栏开始
        .wFromNaviSet(false)
    //导航栏透明度变化
        .wNaviAlphaSet(NO)
    //下划线
        .wInsertMenuLineSet(^(UIView *bgView) {
            //修改内置线
            CGRect rect = bgView.frame;
            if (titleArray.count == 0) {
                rect.size.height = 0;
            }
            else{
                rect.size.height = 1;
            }
            rect.origin.y -= 1;
            
            bgView.frame = rect;
            bgView.backgroundColor = RBG(244, 244, 244);
        })
    //头部
        .wMenuHeadViewSet(^UIView *{
            return self.headBgView;
        });

    self.param = param;
    
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(fkNavBarH);
        make.top.equalTo(self.navView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.upSc.layer.masksToBounds = YES;
        self.upSc.layer.cornerRadius = 0;
    });
}
//获取所有bottom字标题
- (NSArray *)getTabHeadViewAllTitle{
    NSMutableArray *titleArray = [[NSMutableArray alloc]init];
    [titleArray addObject:@"所有媒体"];
    for (NSDictionary *dict in self.dataSource) {
        NSString *pid_name = dict[@"pid_name"];
        if (![titleArray containsObject:pid_name]) {
            [titleArray addObject:pid_name];
        }
    }
    return titleArray;
}
///获取字典中content数组数据
- (NSArray *)getClassContentArray:(NSInteger)section{
    if (section == 0) {
        NSMutableArray *conArray = [[NSMutableArray alloc]init];
        for (NSDictionary *dict in self.dataSource) {
            NSArray *list = dict[@"list"];
            [conArray addObjectsFromArray:list];
        }
        return conArray;
    }
    NSDictionary *conDict = self.dataSource[section];
    NSArray *list = conDict[@"list"];
    return list;;
}
-(void)leftBtnAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    if (self.navigationController.viewControllers.count == 1) {
        if ([ICMusicPlayManager sharedManager].narrowView) {
            [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - GCtabbarHeight, SCREEN_WIDTH, 50);
        }
    }
}
#pragma mark --NetWork
//获取老师内容数据
-(void)getTearchListData{
    [self.view showBusyHUD];
    NSString *teacher_name = self.dataDict[@"teacher_name"];
    NSString *url = [NSString stringWithFormat:@"%@/api/index/AudioList?media_class_id=%@&keyword=%@",server_addr,self.medilassID,teacher_name];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.objectAFNet getUrlStr:url finish:^(id  _Nonnull dataString) {
        [self.view hideBusyHUD];
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        id video = dic[@"data"][@"media"];
        if ([video isKindOfClass:[NSArray class]]) {
            self.dataSource = (NSArray *)video;
        }
        [self createPageView];
    } andError:^(NSError * _Nonnull error) {
        [self.view hideBusyHUD];
    }];
}
#pragma mark --lazy
- (TeadcherHeadView *)headBgView{
    if (!_headBgView) {
        _headBgView = [[TeadcherHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170)];
        _headBgView.teachDict = self.dataDict;
    }
    return _headBgView;
}
//空视图
- (EmptyView *)emptyView {
    
    if (!_emptyView) {
        
        _emptyView = [[EmptyView alloc]init];
        _emptyView.backgroundColor = [UIColor clearColor];
        _emptyView.hidden = YES;
    }
    return _emptyView;
}
@end

